﻿using System;
using System.Collections.Generic;
using System.Text;
using Zhuang.Utility.Common.DataBase;
using System.Collections;
using System.Data;

namespace Zhuang.DAL.Access
{
    public class Pos_CurrentOrderDAL
    {
        public void Add(int productId)
        {
            
            ///ArrayList alSql = new ArrayList();

            string strSql = string.Format("Select count(*) from Pos_CurrentOrder where ProductId={0}",productId);

            DataTable dt = OleDbHelper.Query(strSql).Tables[0];

            if (Convert.ToInt32(dt.Rows[0][0]) > 0)
            {
                strSql = string.Format("Update Pos_CurrentOrder set Quantity=Quantity+1 where ProductId={0}", productId);
            }
            else
            {
                strSql = @"insert into Pos_CurrentOrder(ProductId,ProductName,ProductCode,RetailPrice,UnitPrice,Quantity)
                select ProductId,ProductName,ProductCode,RetailPrice,RetailPrice,1 from Product where ProductId={0}";
                strSql = string.Format(strSql, productId);
            }

            OleDbHelper.ExecuteSql(strSql);

            //OleDbHelper.ExecuteSqlTran(alSql);         
        }

        public void AddByPendingOrder(int PendingOrderId)
        {
            ArrayList alSql = new ArrayList();

            string strSql = @"Delete from Pos_CurrentOrder ";
            alSql.Add(strSql);

            strSql = @"Insert into Pos_CurrentOrder (ProductId,ProductName,ProductCode,UnitPrice,Quantity)
            select ProductId,ProductName,ProductCode,UnitPrice,Quantity from Pos_PendingOrderDetail where PendingOrderMasterId={0}";
            strSql = string.Format(strSql, PendingOrderId);
            alSql.Add(strSql);

            strSql = @"Delete from Pos_PendingOrderMaster where PendingOrderMasterId={0}";
            strSql = string.Format(strSql,PendingOrderId);
            alSql.Add(strSql);

            strSql = @"delete from Pos_PendingOrderDetail where PendingOrderMasterId={0}";
            strSql = string.Format(strSql, PendingOrderId);
            alSql.Add(strSql);

            OleDbHelper.ExecuteSqlTran(alSql);
            
        }


        public DataTable Get()
        {
            string strSql = "select * from Pos_CurrentOrder";

            return OleDbHelper.Query(strSql).Tables[0] ;


        }

        public void Delete(int id)
        {
            string strSql = "Delete from Pos_CurrentOrder where CurrentOrderId={0}" ;
            strSql = string.Format(strSql,id);
            OleDbHelper.ExecuteSql(strSql);
        }
        public void Delete()
        {
            string strSql = " Delete from Pos_CurrentOrder ";
            OleDbHelper.ExecuteSql(strSql);
        }


        public void AddProductQuantity( int productId)
        {
            string strSql = string.Format(@"update Pos_CurrentOrder set Quantity=Quantity+1 where ProductId={0}",productId);
            OleDbHelper.ExecuteSql(strSql);

        }

        public void SubtractProductQuantity(int productId)
        {
            string strSql = string.Format(@"update Pos_CurrentOrder set Quantity=Quantity-1 where ProductId={0}", productId);
            OleDbHelper.ExecuteSql(strSql);
        }

    }
}
